/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "board.h"
#include "at.h"

#define  LED0_PORT        (PortE)
#define  LED0_PIN         (Pin06)

#define  KEY0_PORT        (PortD)
#define  KEY0_PIN         (Pin03)

void float_test(void);

int main(void)
{
    int count = 1;
    
    float_test();
    
    /* enable LED0 */
    stc_port_init_t stcPortInit;
    /* configuration structure initialization */
    MEM_ZERO_STRUCT(stcPortInit);

    stcPortInit.enPinMode = Pin_Mode_Out;
    stcPortInit.enExInt = Disable;
    stcPortInit.enPullUp = Enable;

    /* LED0 Port/Pin initialization */
    PORT_Init(LED0_PORT, LED0_PIN, &stcPortInit);
    
    stcPortInit.enPinMode = Pin_Mode_In;
    /* KEY0 Port/Pin initialization */
    PORT_Init(KEY0_PORT, KEY0_PIN, &stcPortInit);

    while(count++)
    {
        PORT_SetBits(LED0_PORT, LED0_PIN);
        rt_thread_mdelay(1000);
        PORT_ResetBits(LED0_PORT, LED0_PIN);
        rt_thread_mdelay(1000);  
    }
    return 0;
}

void float_test(void)
{
    float a = 0.111;
    float b = 0.222;
    float c;
    
    for (int i = 0; i < 10000000; i++)
    {
        c = a * b;
    }
    c = c;
}
MSH_CMD_EXPORT(float_test, test float);

rt_size_t size = 0;
rt_device_t dev = RT_NULL;

void uart4_test(void)
{
    rt_uint32_t wbuf = 0;
    rt_uint32_t rbuf = 0;

    if (dev == RT_NULL)
    {
        dev = rt_device_find("uart4");
        rt_device_open(dev, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX);
    }
    
    for (int i = 0; i < 10000; i++)
    {
        size = rt_device_write(dev, -1, &wbuf, 1);
        rt_kprintf("write size = %d\n", size);

        size = rt_device_read(dev, -1, &rbuf, 1);
        rt_kprintf("read size = %d, rbuf = %d\n", size, rbuf);
        wbuf++;
    }

    if (dev == RT_NULL)
    {
        rt_device_close(dev);
        dev = RT_NULL;
    }
}
MSH_CMD_EXPORT(uart4_test, ...);
